/**
 * ReduceByKeyTestJava.java
 *
 * @author: ZhuJiahui
 * @date: 2019/3/10 21:05
 */
package six;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class ReduceByKeyTestJava {

    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("ReduceByKeyTest");
        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Tuple2<String, Integer>> list = Arrays.asList(
                new Tuple2<String, Integer>("武当", 99),
                new Tuple2<String, Integer>("少林", 97),
                new Tuple2<String, Integer>("武当", 89),
                new Tuple2<String, Integer>("少林", 77)
        );

        JavaPairRDD<String, Integer> listRDD = sc.parallelizePairs(list);
        JavaPairRDD<String, Integer> result = listRDD.reduceByKey(
                new Function2<Integer, Integer, Integer>() {
                    @Override
                    public Integer call(Integer v1, Integer v2)
                            throws Exception {
                        return v1 + v2;
                    }
                });
        result.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            @Override
            public void call(Tuple2<String, Integer> tuple)
                    throws Exception {
                System.out.println("门派： " + tuple._1 + " -> " + tuple._2);
            }
        });
    }
}
